package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.model.RepayRecord;
import com.cardbaobao.cardbabyclient.model.result.RepayRecordResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.RepaymentRecordAdapter;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.cardbaobao.cardbabyclient.widget.refresh.FooterLoadMore;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description: 还款记录
 * Created by FHW
 * on 2017/11/10
 */

public class RepaymentRecordActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar title_bar;

    @BindView(R.id.sr_layout)
    SmartRefreshLayout sr_layout;
    @BindView(R.id.rv_repay_record)
    RecyclerView rv_repay_record;
    @BindView(R.id.footerLoadMore)
    FooterLoadMore footerLoadMore;

    private int page = 1;
    private List<RepayRecord> repayRecordList;
    private RepaymentRecordAdapter recordAdapter;
    private String credit_card_no = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_repayment_record;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mCurrentPageId= Self.Page.app_repayment_records;

        title_bar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        repayRecordList = new ArrayList<>();

        credit_card_no = getIntent().getStringExtra("credit_card_no");

        footerLoadMore.setStatePreLoadingStr("上拉加载更多");
        sr_layout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                page++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                repayRecordList.clear();
                page = 1;
                loadData();
            }
        });

//        for (int i = 0; i < 20; i++) {
//            RepayRecord repayRecord = new RepayRecord();
//            repayRecordList.add(repayRecord);
//        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv_repay_record.setLayoutManager(linearLayoutManager);
        recordAdapter = new RepaymentRecordAdapter(this, repayRecordList);
        rv_repay_record.setAdapter(recordAdapter);

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        sr_layout.autoRefresh();
    }

    private void loadData() {
        Api.getCreditRecordData(String.valueOf(mApplication.getUser().getID()), credit_card_no)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        completeRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                getString(R.string.string_request_server_fail));
                    }

                    @Override
                    public void onNext(String s) {
                        completeRefresh();
                        RepayRecordResult result = JsonUtil.json2Object(s, RepayRecordResult.class);
                        boolean canLoadMore = true;
                        if (result.getCode() == Constant.SUC_CODE) {
                            if (result.getData() != null&&result.getData().size()>0&&result.getData().get(0).getRepayRecordsList()!=null) {
                                repayRecordList.addAll(result.getData());
                                if (repayRecordList != null && repayRecordList.size() > 0) {
                                    recordAdapter.notifyDataSetChanged();
                                    canLoadMore = true;
                                } else {
                                    //无更多数据
                                    canLoadMore = false;
                                }
                            }
                        } else {
                            dismissProgressDialog();
                            ToastUtil.showToast(result.getMsg());
                        }
//                        sr_layout.setLoadmoreFinished(!canLoadMore);
                        sr_layout.setLoadmoreFinished(true);
                    }
                });
    }

    private void completeRefresh() {
        if (sr_layout.isRefreshing()) {
            sr_layout.finishRefresh();
        }
        if (sr_layout.isLoading()) {
            sr_layout.finishLoadmore();
        }
    }

}
